package com.lion.appletrip.fragment;

import java.util.ArrayList;
import java.util.List;

import com.android.volley.VolleyError;
import com.lion.appletrip.CitySelectActivity;
import com.lion.appletrip.R;
import com.lion.appletrip.WebActivity;
import com.lion.appletrip.model.fg2location.CityData;
import com.lion.appletrip.model.fg2location.LocationData;
import com.lion.appletrip.model.hunter.Hunterdata;
import com.lion.appletrip.model.hunter.ProductList;
import com.lion.appletrip.utils.ApiClient;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.romainpiel.titanic.library.Titanic;
import com.romainpiel.titanic.library.TitanicTextView;
import com.xinbo.utils.GsonUtils;
import com.xinbo.utils.UILUtils;
import com.xinbo.utils.VolleyListener;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class Fragment2 extends Fragment
{

	private LayoutInflater inflater;
	private MyAdapter adapter;
	private List<ProductList> productList = new ArrayList<ProductList>();
	private int size = 4;
	private View btnMore;
	private View lion002;
	private TitanicTextView ivLoading;
	private Titanic titanic;
	private View citySelect;
	private TextView mTvCity;
	public Fragment2()
	{
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
	        Bundle savedInstanceState)
	{
		
		this.inflater = inflater;
		View layout = inflater.inflate(R.layout.fragment_fragment2, container,
		        false);
		ListView mListView = (ListView) layout
		        .findViewById(R.id.hunter_listview_fg2);
		mTvCity = (TextView) layout.findViewById(R.id.tv_name_city);
		ivLoading = (TitanicTextView) layout.findViewById(R.id.loading_fg2);
		lion002 = layout.findViewById(R.id.lion002);
		View footview = inflater.inflate(R.layout.fragment_fragment2_hunter_footview, null);
		btnMore = footview.findViewById(R.id.btn_more);
		titanic = new Titanic();
		titanic.start(ivLoading );
		initData();
		mListView.addFooterView(footview,null,false);
		adapter = new MyAdapter();
		mListView.setAdapter(adapter);
		citySelect = layout.findViewById(R.id.city_fg2);
		btnMore.setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				if (size<productList.size()-2)
                {
					size+=5;
                }else {
                	btnMore.setVisibility(View.GONE);
                	size=productList.size();
				}
				adapter.notifyDataSetChanged();
			}
		});
		mListView.setOnItemClickListener(new OnItemClickListener()
		{

			@Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id)
            {
					ProductList productList2 = productList.get(position);
					Integer productId = productList2.getProductId();
					String html="http://web.breadtrip.com/hunter/product/"+productId+"/?bts=app_tab";
					Intent intent = new Intent();
					intent.putExtra("html",html);
					intent.setClass(getContext(), WebActivity.class);
					startActivity(intent);
            }
		});
		return layout;
	}
	@Override
	public void onResume()
	{
	    adapter=new MyAdapter();
	    super.onResume();
	}
	private void initData()
	{
		ApiClient.HUNTRT_XIAMENGet(getContext(), new VolleyListener()
		{

			@Override
			public void onResponse(String arg0)
			{
				Hunterdata hunterdata = GsonUtils.parseJSON(arg0,
				        Hunterdata.class);
				productList.clear();
				productList = hunterdata.getProductList();
				adapter.notifyDataSetChanged();
				titanic.cancel();
				ivLoading.setVisibility(View.GONE);
				lion002.setVisibility(View.VISIBLE);
				if (size>=productList.size())
                {
					btnMore.setVisibility(View.GONE);
                }
            	
			}

			@Override
			public void onErrorResponse(VolleyError arg0)
			{
				// TODO Auto-generated method stub

			}
		});
		ApiClient.DownCityFg2(getContext(), new VolleyListener()
		{
			
			@Override
			public void onResponse(final String json)
			{
				LocationData locationData = GsonUtils.parseJSON(json, LocationData.class);
				final CityData cityData = locationData.getCityData();
				citySelect.setOnClickListener(new OnClickListener()
				{
					
					@Override
					public void onClick(View v)
					{
						Intent intent =  new Intent();
						intent.putExtra("cityData", json);
						intent.setClass(getContext(),CitySelectActivity.class);
						startActivityForResult(intent,0);
					}
				});
			}
			
			@Override
			public void onErrorResponse(VolleyError arg0)
			{
				// TODO Auto-generated method stub
				
			}
		});

	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (data != null)
		{
			String cityName = data.getStringExtra("CityName");
			if (cityName != null)
			{
				mTvCity.setText(cityName);
			}
		}
	    super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	
	
	class MyAdapter extends BaseAdapter
	{

		private List<String> tabList = new ArrayList<String>();
		
		@Override
		public int getCount()
		{
			int num = 0;
			if (productList.size() > 0)
			{
				num = size;
			}
			return num;
		}

		@Override
		public Object getItem(int position)
		{
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position)
		{
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View layout = null;
			ViewHolder viewHolder = null;

			if (convertView == null)
			{
				viewHolder = new ViewHolder();
				layout = inflater.inflate(
				        R.layout.fragment_fragment2_hunter_item, null);
				viewHolder.ivTitlePager = (ImageView) layout
				        .findViewById(R.id.iv_title_pager);
				viewHolder.ivAvatarL = (ImageView) layout
				        .findViewById(R.id.iv_avatar_l);
				viewHolder.tvTitle = (TextView) layout
				        .findViewById(R.id.tv_title);
				viewHolder.tvAddress = (TextView) layout
				        .findViewById(R.id.tv_address);
				viewHolder.tvDistance = (TextView) layout
				        .findViewById(R.id.tv_distance);
				viewHolder.tvLikeCount = (TextView) layout
				        .findViewById(R.id.tv_like_count);
				viewHolder.tvPrice = (TextView) layout
				        .findViewById(R.id.tv_price);
				viewHolder.tvTableList = (TextView) layout
				        .findViewById(R.id.tv_table_list);
				viewHolder.tvTableList2 = (TextView) layout
				        .findViewById(R.id.tv_table_list2);
				viewHolder.tvTableList3 = (TextView) layout
				        .findViewById(R.id.tv_table_list3);
				viewHolder.defaultIn = layout.findViewById(R.id.lion007);
				layout.setTag(viewHolder);
			} else
			{
				layout = convertView;
				viewHolder = (ViewHolder) layout.getTag();
			}

			ProductList mData = productList.get(position);
			UILUtils.displayImageNoAnim(mData.getTitlePage(),
			        viewHolder.ivTitlePager);
			UILUtils.displayImageNoAnim(mData.getUser().getAvatarL(),
			        viewHolder.ivAvatarL);
			viewHolder.tvTitle.setText(mData.getTitle());
			viewHolder.tvAddress.setText(mData.getAddress());
			if ("".equals(mData.getAddress())
			        && "".equals(mData.getLikeCount())
			        && "".equals(mData.getDistance()))
			{
				viewHolder.defaultIn.setVisibility(View.GONE);
			}
			viewHolder.tvLikeCount.setText(mData.getLikeCount() + "人喜欢");
			viewHolder.tvPrice.setText("￥" + mData.getPrice());
			viewHolder.tvDistance.setText(mData.getDistance());
			tabList.clear();
			tabList = mData.getTabList();
			if (tabList.size() > 0)
			{
				if (tabList.size() >= 1 && tabList.size() < 2)
				{
					viewHolder.tvTableList.setVisibility(View.VISIBLE);
					viewHolder.tvTableList.setText(tabList.get(0));
				} else if (tabList.size() >= 2 && tabList.size() < 3)
				{
					viewHolder.tvTableList.setVisibility(View.VISIBLE);
					viewHolder.tvTableList2.setVisibility(View.VISIBLE);
					viewHolder.tvTableList.setText(tabList.get(0));
					viewHolder.tvTableList2.setText(tabList.get(1));
				} else if (tabList.size() >= 3)
				{
					viewHolder.tvTableList.setVisibility(View.VISIBLE);
					viewHolder.tvTableList2.setVisibility(View.VISIBLE);
					viewHolder.tvTableList3.setVisibility(View.VISIBLE);
					viewHolder.tvTableList.setText(tabList.get(0));
					viewHolder.tvTableList2.setText(tabList.get(1));
					viewHolder.tvTableList3.setText(tabList.get(2));
				}
			}

			return layout;
		}

	}
	@Override
	public void onStop()
	{
	    ImageLoader.getInstance().clearMemoryCache();
	    super.onStop();
	}
	@Override
	public void onDestroy()
	{
	    ImageLoader.getInstance().clearMemoryCache();
	    super.onDestroy();
	}
	private class ViewHolder
	{
		private ImageView ivTitlePager;
		private ImageView ivAvatarL;
		private TextView tvTitle;
		private TextView tvAddress;
		private TextView tvDistance;
		private TextView tvLikeCount;
		private TextView tvPrice;
		private TextView tvTableList;
		private TextView tvTableList2;
		private TextView tvTableList3;
		private View defaultIn;
	}

}
